package E2013;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class e14 extends JFrame{

    public e14(){

        //设置窗体基本布局
        this.setSize(300,400);
        this.setLocation(300,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);

        JPanel jPanel = new JPanel();

        //创建文本框,并添加监听器
        JTextField jt1 = new JTextField(20);
        JTextField jt2 = new JTextField(20);
        jt2.setEditable(false);
        JButton btn = new JButton();
        jt1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {

                StringBuffer s = new StringBuffer();
                //判断输入的是否是回车键
                if(e.getKeyChar() == '\n'){
                    btn.doClick();
                    for(int i=0;i<3;i++){
                        s.append(jt1.getText()+" ");
                    }
                }
                jt2.setText(String.valueOf(s)) ;
            }
        });
        jt1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
            }
        });

        //创建按钮并为其添加监听器
        JButton jButton = new JButton("清空");
        jButton.addActionListener(e ->{
            String content1 = jt1.getText();
            String content2 = jt2.getText();
            jt1.setText("");
            jt2.setText("");
        });

        jPanel.add(jt1);
        jPanel.add(jt2);
        jPanel.add(jButton);
        this.add(jPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new e14();
            }
        });
    }
}
